<?php

class NewsController extends Zend_Controller_Action {

    public function preDispatch() {
        if (!defined('PAGE_ID'))
            $this->_redirect(SITE_URL);
    }

    /**
     * Отображение секции фронт енд
     */
    public function indexAction() {
        $sid = (int) $this->getRequest()->getParam('sid', 0);
        $nid = (int) $this->getRequest()->getParam('post', 0);
        $page = (int) $this->getRequest()->getParam('page', 1);
        //print_r($nid);exit;
        $model = new NewsModel();
        if ($nid > 0) {
            $this->view->news = $model->getNews($sid, $nid);
            $breadcrumbs = Zend_Registry::set('breadcrumbs',array(array('name' => $this->view->news->title, 'url' => PAGE_URL . '/nid/' . $nid . '/page/' . $page)));
            $this->view->full = true;
            $this->view->currentpage = $page;
            $this->view->page_title = $this->view->news->title;
            $this->view->page_keywords = $this->view->news->keywords;
            $this->view->page_description = $this->view->news->description;
        } else if ($sid > 0) {
            $this->view->news = $model->getPaginatorRows($page, $sid);
            $this->view->full = false;
            $this->view->paginator = $this->view->news->getPages();
        }
    }

    public function widgetAction() {
        $model = new PageModel();
        $sid = (int) $this->getRequest()->getParam('sid', 0);
        $count = (int) $this->getRequest()->getParam('count', 0);
        $section = $model->getSection($sid);
        $pages = Zend_Registry::get('pages');
        //$this->view->page->url = $pages[$section->pid]->url;
        $news_model = new NewsModel();
        $this->view->news = $news_model->getNews($sid, 0, $count);
        $this->view->full = false;
    }

    /**
     * Правка
     */
    public function modifyAction() {
        /*
         * Разбиваем по контролеррам в зависимости от действия
         */
        $action = $this->getRequest()->getParam('a', '');
        switch ($action) {
            case 'edit':
                $this->_forward('news');
                break;
            case 'delete':
                $this->_forward('delete_news');
                break;
            default:
                (int) $sid = $this->getRequest()->getParam('id', 0);
                $model = new NewsModel();
                $this->view->news = $model->getNews($sid);
        }
    }

    /*
     * Добавление секции
     */

    public function addAction() {
        $pid = (int) $this->getRequest()->getParam('pid', 0);
        $this->_redirect('/pages/section/id/' . $pid);
    }

    /*
     * Удаление определенной новости
     */

    public function deletenewsAction() {
        $model = new NewsModel();
        $sid = (int) $this->getRequest()->getParam('sid', 0);
        $nid = (int) $this->getRequest()->getParam('nid', 0);
        $model->deleteNews($nid);
        $this->_redirect('/pages/modify/id/' . $sid);
    }

    /*
     * Правка новости
     */

    public function newsAction() {
        $t = Zend_Registry::get('Zend_Locale');
        $sid = (int) $this->getRequest()->getParam('id', 0);
        $nid = (int) $this->getRequest()->getParam('nid', 0);
        $model = new NewsModel();
        $forms = new NewsForm();
        $form = $forms->News($sid, $nid);

        if ($this->_request->isPost()) {//Форма отправлена
            $formData = $this->_request->getPost();
            if ($form->isValid($formData)) {
                $auth = Zend_Auth::getInstance();
                $uid = $auth->getIdentity()->id;
                $saveData = array(
                    'title' => $formData['title'],
                    'date' => date('Y-m-d', strtotime($formData['date'])),
                    'author' => $uid,
                    'preview' => $formData['preview'],
                    'short' => $formData['short'],
                    'long' => $formData['long'],
                    'keywords' => $formData['keywords'],
                    'description' => $formData['description']
                );
                $nid = $model->updateNews($saveData, $sid, $nid);
                $this->_redirect('/pages/modify/id/' . $sid . '/nid/' . $nid . '/a/edit');
            } else {
                $form->populate($formData);
            }
        }
        if ($nid > 0) {
            $this->view->news = $model->getNews($sid, $nid);
            $this->view->navigation_line = $this->view->navigation_line + array($this->view->news->title => ADMIN_URL . '/pages/modify/id/' . $sid . '/a/edit/nid/' . $nid);
            $form->populate(array(
                'title' => $this->view->news->title,
                'date' => date('d.m.Y', strtotime($this->view->news->date)),
                'preview' => $this->view->news->preview,
                'short' => $this->view->news->short,
                'long' => $this->view->news->long,
                'keywords' => $this->view->news->keywords,
                'description' => $this->view->news->description
            ));
        } else {
            $this->view->navigation_line = $this->view->navigation_line + array($t->_('New post') => ADMIN_URL . '/pages/modify/id/' . $sid . '/a/edit/');
        }
        $this->view->form = $form;
    }

}
